package com.baizhi.controller;

import com.baizhi.entrty.Banner;
import com.baizhi.service.BannerService;
import com.baizhi.service2.BannerService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class BannerController {
    @Autowired
    private BannerService bs;

    @Autowired
    private BannerService2 bs2;


    @RequestMapping("fyb")
    @ResponseBody
    public Map fy(int page, int rows){

        return bs.fy(page,rows);
    }


    @RequestMapping("xs")
    @ResponseBody
    public Map dj(int[] id){
        System.out.println(id+"---------------------------");
        System.out.println(id+"---------------------------");
        System.out.println(id+"---------------------------");
        return  bs2.xs(id);
    }

    @RequestMapping("bxs")
    @ResponseBody
    public Map jd(int[] id){

        return bs2.bxs(id);
    }

    @RequestMapping("insertb")
    @ResponseBody
    public Map insert(Banner ba){
        return bs2.insert(ba);
    }

    @RequestMapping("updateb")
    @ResponseBody
    public Map update(Banner ba){
        System.out.println(ba+"------");
        System.out.println(ba+"------");
        System.out.println(ba+"------");
        return bs2.update(ba);
    }

    @RequestMapping("/addBanner")
    @ResponseBody
    public Map addBanner(Banner banner, MultipartFile photo, HttpServletRequest request) throws IOException {

        System.out.println(banner+"-----------");
        System.out.println(banner+"-----------");
        System.out.println(banner+"-----------");
        System.out.println(banner+"-----------");
        //System.out.println(photo+"photo");

        String originalFileName = photo.getOriginalFilename();

        //System.out.println(originalFileName+"originalFileName");
        String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
        //System.out.println(suffix+"suffix");

        String realPath = request.getServletContext().getRealPath("/img");
        String uuid = UUID.randomUUID().toString();
        String newPhoto = uuid + suffix;

        photo.transferTo(new File(realPath + "/" +newPhoto));

        banner.setBannerImage("/img/"+newPhoto);

        System.out.println(banner + "banner");
        Map map = new HashMap();
        try {
            bs.addBanner(banner);
            map.put("myadd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("myadd", false);
        }
        return map;
    }



}
